import re
from re import search

import allure
import pytest
import requests
import yaml

class TestApi:
    csrf_token = ""
    sess = requests.session()       # 在类中定义会话对象，因为所以方法都得调用这同一个类

    # 获取token值
    def test_phpwind(self):
        urls = "http://47.107.116.139/phpwind/"
        res = TestApi.sess.request(method="get", url=urls)
        #print(res.text)
        # 从返回的信息中找到token值 截取下来
        search_value = re.search('name="csrf_token" value="(.*?)"', res.text)
        TestApi.csrf_token = search_value.group(1)
        print(TestApi.csrf_token)
        assert  "本站新帖" in res.text

    def test_login(self):
        urls = "http://47.107.116.139/phpwind/index.php?m=u&c=login&a=dorun"
        datas = {
            "username":"levi",
            "password":"123123",
            "csrf_token":TestApi.csrf_token,
            "backurl":"http://47.107.116.139/phpwind/",
            "invite":""
        }
        headerss={
            "Accept":"application/json, text/javascript, /; q=0.01",
            "X-Requested-with":"XMLHttpRequest"
        }
        # 不能用requests.post()因为该接口存在cookie依赖，所以只能用session
        #res = requests.post(url=urls, data=datas,header=headerss)
        res = TestApi.sess.request(method="post", url=urls, data=datas, headers=headerss)
        print(res.json())